package com.ruoyi.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.store.bo.HmStoreProductAddBo;
import com.ruoyi.store.bo.HmStoreProductQueryBo;
import com.ruoyi.store.bo.HmStoreProductEditBo;
import com.ruoyi.store.domain.HmStoreProduct;
import com.ruoyi.store.mapper.HmStoreProductMapper;
import com.ruoyi.store.vo.HmStoreProductVo;
import com.ruoyi.store.service.IHmStoreProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-02
 */
@Service
public class HmStoreProductServiceImpl extends ServicePlusImpl<HmStoreProductMapper, HmStoreProduct> implements IHmStoreProductService {

    @Override
    public HmStoreProductVo queryById(Long id){
        return getVoById(id, HmStoreProductVo.class);
    }

    @Override
    public TableDataInfo<HmStoreProductVo> queryPageList(HmStoreProductQueryBo bo) {
        PagePlus<HmStoreProduct, HmStoreProductVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HmStoreProductVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<HmStoreProductVo> queryList(HmStoreProductQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HmStoreProductVo.class);
    }

    private LambdaQueryWrapper<HmStoreProduct> buildQueryWrapper(HmStoreProductQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmStoreProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMerId() != null, HmStoreProduct::getMerId, bo.getMerId());
        lqw.eq(StrUtil.isNotBlank(bo.getImage()), HmStoreProduct::getImage, bo.getImage());
        lqw.eq(StrUtil.isNotBlank(bo.getSliderImage()), HmStoreProduct::getSliderImage, bo.getSliderImage());
        lqw.like(StrUtil.isNotBlank(bo.getStoreName()), HmStoreProduct::getStoreName, bo.getStoreName());
        lqw.eq(StrUtil.isNotBlank(bo.getStoreInfo()), HmStoreProduct::getStoreInfo, bo.getStoreInfo());
        lqw.eq(StrUtil.isNotBlank(bo.getKeyword()), HmStoreProduct::getKeyword, bo.getKeyword());
        lqw.eq(StrUtil.isNotBlank(bo.getBarCode()), HmStoreProduct::getBarCode, bo.getBarCode());
        lqw.eq(StrUtil.isNotBlank(bo.getCateId()), HmStoreProduct::getCateId, bo.getCateId());
        lqw.eq(bo.getPrice() != null, HmStoreProduct::getPrice, bo.getPrice());
        lqw.eq(bo.getVipPrice() != null, HmStoreProduct::getVipPrice, bo.getVipPrice());
        lqw.eq(bo.getOtPrice() != null, HmStoreProduct::getOtPrice, bo.getOtPrice());
        lqw.eq(bo.getPostage() != null, HmStoreProduct::getPostage, bo.getPostage());
        lqw.like(StrUtil.isNotBlank(bo.getUnitName()), HmStoreProduct::getUnitName, bo.getUnitName());
        lqw.eq(bo.getSort() != null, HmStoreProduct::getSort, bo.getSort());
        lqw.eq(bo.getSales() != null, HmStoreProduct::getSales, bo.getSales());
        lqw.eq(bo.getStock() != null, HmStoreProduct::getStock, bo.getStock());
        lqw.eq(bo.getIsShow() != null, HmStoreProduct::getIsShow, bo.getIsShow());
        lqw.eq(bo.getIsHot() != null, HmStoreProduct::getIsHot, bo.getIsHot());
        lqw.eq(bo.getIsBenefit() != null, HmStoreProduct::getIsBenefit, bo.getIsBenefit());
        lqw.eq(bo.getIsBest() != null, HmStoreProduct::getIsBest, bo.getIsBest());
        lqw.eq(bo.getIsNew() != null, HmStoreProduct::getIsNew, bo.getIsNew());
        lqw.eq(bo.getAddTime() != null, HmStoreProduct::getAddTime, bo.getAddTime());
        lqw.eq(bo.getIsPostage() != null, HmStoreProduct::getIsPostage, bo.getIsPostage());
        lqw.eq(bo.getIsDel() != null, HmStoreProduct::getIsDel, bo.getIsDel());
        lqw.eq(bo.getMerUse() != null, HmStoreProduct::getMerUse, bo.getMerUse());
        lqw.eq(bo.getGiveIntegral() != null, HmStoreProduct::getGiveIntegral, bo.getGiveIntegral());
        lqw.eq(bo.getCost() != null, HmStoreProduct::getCost, bo.getCost());
        lqw.eq(bo.getIsSeckill() != null, HmStoreProduct::getIsSeckill, bo.getIsSeckill());
        lqw.eq(bo.getIsBargain() != null, HmStoreProduct::getIsBargain, bo.getIsBargain());
        lqw.eq(bo.getIsGood() != null, HmStoreProduct::getIsGood, bo.getIsGood());
        lqw.eq(bo.getIsSub() != null, HmStoreProduct::getIsSub, bo.getIsSub());
        lqw.eq(bo.getFicti() != null, HmStoreProduct::getFicti, bo.getFicti());
        lqw.eq(bo.getBrowse() != null, HmStoreProduct::getBrowse, bo.getBrowse());
        lqw.eq(StrUtil.isNotBlank(bo.getCodePath()), HmStoreProduct::getCodePath, bo.getCodePath());
        lqw.eq(StrUtil.isNotBlank(bo.getSoureLink()), HmStoreProduct::getSoureLink, bo.getSoureLink());
        lqw.eq(StrUtil.isNotBlank(bo.getVideoLink()), HmStoreProduct::getVideoLink, bo.getVideoLink());
        lqw.eq(bo.getTempId() != null, HmStoreProduct::getTempId, bo.getTempId());
        lqw.eq(bo.getSpecType() != null, HmStoreProduct::getSpecType, bo.getSpecType());
        lqw.eq(StrUtil.isNotBlank(bo.getActivity()), HmStoreProduct::getActivity, bo.getActivity());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(HmStoreProductAddBo bo) {
        HmStoreProduct add = BeanUtil.toBean(bo, HmStoreProduct.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HmStoreProductEditBo bo) {
        HmStoreProduct update = BeanUtil.toBean(bo, HmStoreProduct.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HmStoreProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
